package org.his.controller;

import java.util.List;

import org.his.entity.Drug;
import org.his.entity.Medicine;
import org.his.service.DrugService;
import org.his.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/medicine")
public class MedicineController {
	@Autowired
	private MedicineService medicineService;
	@Autowired
	private DrugService drugService;
	
	@RequestMapping(value = "/addmedicine",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addMedicine(Medicine addMedicine) {
				int count = medicineService.addMedicine(addMedicine);
				if (count > 0) {
					return "{\"success\":\"true\",\"message\":\"用药增加成功\"}";
				}else if(count == -2){
					return "{\"success\":\"flase\",\"message\":\"请勿重复添加用药\"}";
				}else {
					return "{\"success\":\"flase\",\"message\":\"添加用药失败\"}";
				}
			}
		
		//根据药品名把规格赋值到数据中（可选实现）
	
	@RequestMapping(value = "/queryByMedicalrecord")
	@ResponseBody
	public List<Medicine> queryByMedicalrecord(@RequestParam("medicalrecord") String medicalrecord) {
		List<Medicine> aMedicines = medicineService.queryByMedicalrecord(medicalrecord);	
		return aMedicines;
		
	}
	@RequestMapping(value = "/deletemedicine",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String deleteByDrugname(@RequestParam("drugname") String drugname) {
		//根据药品名把规格赋值到数据中（可选实现）
		int count = medicineService.deleteByDrugname(drugname);
				if (count > 0) {
			return "{\"success\":\"true\",\"message\":\"用药删除成功\"}";
		}else if(count == -1) {
			return "{\"success\":\"flase\",\"message\":\"无药品名\"}";
		}else {
			return "{\"success\":\"flase\",\"message\":\"用药删除失败\"}";
		}
	}
}
